using System;
using System.IO;
using System.Collections;
using System.Windows.Forms;

using MSHelpLib.CHM.Decoding;

namespace MSHelpLib
{
	/// <summary>
	/// The class <c>TOCItem</c> implements a toc-entry item
	/// </summary>
	public sealed class TOCItem
	{
		/// <summary>
		/// Constant for standard folder (closed) image index (HH2 image list)
		/// </summary>
		public const int STD_FOLDER_HH2 = 4;
		/// <summary>
		/// Constant for standard folder (opened) image index (HH2 image list)
		/// </summary>
		public const int STD_FOLDER_OPEN_HH2 = 6;
		/// <summary>
		/// Constant for standard file image index (HH2 image list)
		/// </summary>
		public const int STD_FILE_HH2 = 16;

		/// <summary>
		/// Constant for standard folder (closed) image index (HH1 image list)
		/// </summary>
		public const int STD_FOLDER_HH1 = 0;
		/// <summary>
		/// Constant for standard folder (opened) image index (HH1 image list)
		/// </summary>
		public const int STD_FOLDER_OPEN_HH1 = 1;
		/// <summary>
		/// Constant for standard file image index (HH1 image list)
		/// </summary>
		public const int STD_FILE_HH1 = 10;

        /// <summary>
        /// 
        /// </summary>
		public int Tag=0;
		/// <summary>
		/// Internal flag specifying the data extraction mode used for this item
		/// </summary>
		private DataMode _tocMode = DataMode.TextBased;
		/// <summary>
		/// Internal member storing the offset (only used in binary tocs)
		/// </summary>
		private int _offset = 0;
		/// <summary>
		/// Internal member storing the offset of the next item(only used in binary tocs)
		/// </summary>
		private int _offsetNext = 0;
		/// <summary>
		/// Internal member storing a merge link. 
		/// If the target file is in the merged files list of the CHM,
		/// this item will be replaced with the target TOC or Topic, if not it will 
		/// be removed from TOC.
		/// </summary>
		private string _mergeLink = "";
		/// <summary>
		/// Internal member storing the toc name
		/// </summary>
		private string _name = "";
		/// <summary>
		/// Internal member storing the toc loca (content file)
		/// </summary>
		private string _local = "";
		/// <summary>
		/// Internal member storing all associated information type strings
		/// </summary>
		private ArrayList _infoTypeStrings = new ArrayList();
		/// <summary>
		/// Internal member storing the associated chm file
		/// </summary>
		private string _helpFile = "";
		/// <summary>
		/// Internal member storing the image index
		/// </summary>
		private int _imageIndex = -1;
		/// <summary>
		/// Internal member storing the offset of the associated topic entry (for binary tocs)
		/// </summary>
		private int _topicOffset = -1;
		/// <summary>
		/// Internal member storing the toc children
		/// </summary>
		private ArrayList _children = new ArrayList();

        /// <summary>
        /// 
        /// </summary>
		public int TagIndex=0;
		/// <summary>
		/// Internal member storing the parameter collection
		/// </summary>		
		[NonSerialized]		
		private Hashtable _otherParams = new Hashtable();
		/// <summary>
		/// Internal member storing the associated chmfile object
		/// </summary>		
		[NonSerialized]
		private IHelpFile _assFile = null;
				
		[NonSerialized]
		private CHMFile _associatedFile = null;

		/// <summary>
		/// Parent item
		/// </summary>
		[NonSerialized]
		private TOCItem _parent=null;

		/// <summary>
		/// Holds a pointer to the next item in the TOC
		/// </summary>		
		[NonSerialized]
		public TOCItem Next=null;

		/// <summary>
		/// Holds a pointer to the previous item in the TOC
		/// </summary>
		[NonSerialized]
		public TOCItem Prev=null;

		/// <summary>
		/// Holds a pointer to the TreeNode where this TOC Item is used
		/// </summary>
		[NonSerialized]
		public System.Windows.Forms.TreeNode treeNode=null;

		/// <summary>
		/// Constructor of the class used during text-based data extraction
		/// </summary>
		/// <param name="name">name of the item</param>
		/// <param name="local">local content file</param>
		/// <param name="ImageIndex">image index</param>
        /// <param name="helpFileName">associated chm file</param>
		public TOCItem(string name, string local, int ImageIndex, string helpFileName)
		{
			_tocMode = DataMode.TextBased;
			_name = name;
			_local = local;
			_imageIndex = ImageIndex;
			_helpFile = helpFileName;
		}

		/// <summary>
		/// Constructor of the class used during binary data extraction
		/// </summary>
		/// <param name="topicOffset">offset of the associated topic entry</param>
		/// <param name="ImageIndex">image index to use</param>
		/// <param name="associatedFile">associated chm file</param>
		public TOCItem(int topicOffset, int ImageIndex, IHelpFile associatedFile)
		{
			_tocMode = DataMode.Binary;
			_assFile=associatedFile;
			if (associatedFile.GetType()==typeof(CHMFile))
				_associatedFile = (CHMFile)associatedFile;
			else
				_associatedFile = null;

			_helpFile = associatedFile.FInfo.FullName;
			_topicOffset = topicOffset;
			_imageIndex = ImageIndex;
		}

		/// <summary>
		/// Standard constructor
		/// </summary>
		public TOCItem()
		{
		}

		/// <summary>
		/// Gets/Sets the data extraction mode with which this item was created.
		/// </summary>
		internal DataMode TocMode
		{
			get { return _tocMode; }
			set { _tocMode = value; }
		}

		/// <summary>
		/// Gets/Sets the offset of the associated topic entry
		/// </summary>
		internal int TopicOffset
		{
			get { return _topicOffset; }
			set { _topicOffset = value; }
		}

		/// <summary>
		/// Gets/Sets the associated CHMFile instance
		/// </summary>
		internal IHelpFile AssociatedFile
		{
			get { return _associatedFile; }
			set 
			{ 
				_assFile=null;
				_associatedFile = null;
				if (value==null)
					return;

				_assFile=value;

				if (value.GetType()==typeof(CHMFile))				
					_associatedFile = (CHMFile)value;
				else
					_associatedFile = null;

				if (_assFile!=null)
				{
					_helpFile=_assFile.FInfo.FullName;
				}
			}
		}

		/// <summary>
		/// Gets/Sets the offset of the item.
		/// </summary>
		/// <remarks>Only used in binary tocs</remarks>
		internal int Offset
		{
			get	{ return _offset; }
			set	{ _offset = value; }
		}

		/// <summary>
		/// Gets/Sets the offset of the next item.
		/// </summary>
		/// <remarks>Only used in binary tocs</remarks>
		internal int OffsetNext
		{
			get	{ return _offsetNext; }
			set	{ _offsetNext = value; }
		}

		/// <summary>
		/// Gets the ArrayList which holds all information types/categories this item is associated
		/// </summary>
		internal ArrayList InfoTypeStrings
		{
			get { return _infoTypeStrings; }
		}

		/// <summary>
		/// Gets/Sets the parent of this item
		/// </summary>
		public TOCItem Parent
		{
			get { return _parent; }
			set { _parent = value; }
		}

		/// <summary>
		/// Gets/Sets the mergelink for this item. 
		/// <b>You should not set the mergedlink by your own !</b>
		/// This is only for loading merged CHMs.
		/// </summary>
		public string MergeLink
		{
			get { return _mergeLink; }
			set { _mergeLink = value; }
		}

		/// <summary>
		/// Gets/Sets the name of the item
		/// </summary>
		public string Name
		{
			get	
			{ 
				if(_mergeLink.Length > 0)
					return "";

				if(_name.Length <= 0)
				{
					if((_tocMode == DataMode.Binary )&&(_associatedFile!=null))
					{
						if( _topicOffset >= 0)
						{							
							TopicEntry te = (TopicEntry) (_associatedFile.TopicsFile[_topicOffset]);
							if(te != null)
							{
								return te.Title;
							}
						}
					}
				}

				return _name; 
			}
			set	
			{ 
				_name = value; 
			}
		}

		/// <summary>
		/// Gets/Sets the local of the item
		/// </summary>
		public string Local
		{
			get	
			{ 
				if(_mergeLink.Length > 0)
					return "";

				if (_local==null)
					return "";

				if(_local.Length <= 0)
				{
					if((_tocMode == DataMode.Binary )&&(_associatedFile!=null))
					{
						if( _topicOffset >= 0)
						{
							TopicEntry te = (TopicEntry) (_associatedFile.TopicsFile[_topicOffset]);
							if(te != null)
							{
								return te.Locale;
							}
						}
					}
				}

				return _local; 
			}
			set	{ _local = value; }
		}

		/// <summary>
		/// Gets/Sets the chm file
		/// </summary>
		public string HelpFileName
		{
			get	
			{ 
				if(_associatedFile!=null)
					return _associatedFile.FileName;

				return _helpFile; 
			}
			set	{ _helpFile = value; }
		}

		/// <summary>
		/// Gets the url for the webbrowser for this file
		/// </summary>
		public string Url
		{
			get
			{
				string sL = Local;

				if( (sL.ToLower().IndexOf("http://") >= 0) ||
					(sL.ToLower().IndexOf("https://") >= 0) ||
					(sL.ToLower().IndexOf("mailto:") >= 0) ||
					(sL.ToLower().IndexOf("ftp://") >= 0) || 
					(sL.ToLower().IndexOf("file://") >= 0) || 
					(sL.ToLower().IndexOf("ms-its:") >= 0))
					return sL;

				// Remove Leading Slash
				if ((sL!=null) && (sL.Length>2) && ((sL.StartsWith("/")) || (sL.StartsWith(@"\"))))
					sL=sL.Substring(1);

				if (_assFile!=null)
					return this._assFile.UrlPrefix + _helpFile + "::/" + sL;
				else
					return _helpFile + "::/" + sL;
			}
		}

		/// <summary>
		/// Gets/Sets the image index of the item
		/// </summary>
		/// <remarks>Set this to -1 for a default icon</remarks>
		public int ImageIndex
		{
			get	
			{ 
				return _imageIndex; 
			}
			set	{ _imageIndex = value; }
		}

		/// <summary>
		/// Gets/Sets the children of this item.
		/// </summary>
		/// <remarks>Each entry in the ArrayList is of type TOCItem</remarks>
		public ArrayList Children
		{
			get { return _children; }
			set { _children = value; }
		}

		/// <summary>
		/// Gets the internal hashtable storing all params
		/// </summary>
		public Hashtable Params
		{
			get { return _otherParams; }
		}

        /// <summary>
        /// Saves to stream.
        /// </summary>
        /// <param name="st">The st.</param>
		public void SaveToStream(BinaryWriter st)
		{										
			st.Write(this.Name);
			st.Write(this.Local);
			st.Write(this.MergeLink);

			// If we are a merge link then do not save children - They will be loaded later.
			if (this.MergeLink!="")
			{
				st.Write((Int32)0);	
				return;
			}

			// Else save children as normal			
			st.Write((Int32)this.Children.Count);	
			foreach(TOCItem item in this.Children)
			{
				item.SaveToStream(st);
			}
		}

        /// <summary>
        /// Loads from stream.
        /// </summary>
        /// <param name="st">The st.</param>
        /// <param name="parent">The parent.</param>
        /// <param name="helpFile">The help file.</param>
		public void LoadFromStream(BinaryReader  st, TOCItem parent, IHelpFile helpFile)
		{			
			this.AssociatedFile=helpFile;
			this._name=st.ReadString();			
			this._local=st.ReadString();
			this._mergeLink=st.ReadString();
			this._parent=parent;
			int ChildCount=st.ReadInt32();
			for(int i=0;i<ChildCount;i++)
			{
				TOCItem item=new TOCItem();
				item.Parent=this;
				Children.Add(item);
				item.LoadFromStream(st,item,helpFile);
			}
			if (_mergeLink!="")
				helpFile.MergeLinks.Add(this);
		}
	}
}
